package com.sh.wy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.util.Result;
import com.sh.wy.fee.entity.FeePower;
import com.sh.wy.fee.entity.FeeWater;
import com.sh.wy.fee.service.IFeePowerService;
import com.sh.wy.fee.service.IFeeWaterService;
import com.sh.wy.system.entity.*;
import com.sh.wy.system.service.ILiveHouseService;
import com.sh.wy.system.service.ILiveRoleService;
import com.sh.wy.system.service.ILiveUserService;
import com.sh.wy.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业主表 前端控制器
 * </p>
 *
 * @author 世豪学Java
 * @since 2023-12-14
 */
@RestController
@RequestMapping("/api/liveUser")
@Api(tags = "业主控制类")
public class LiveUserController {
    @Autowired
    private ILiveUserService liveUserService;

    @Autowired
    private IFeePowerService feePowerService;

    @Autowired
    private IFeeWaterService feeWaterService;

    @Autowired
    private ILiveHouseService liveHouseService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation("业主列表")
    @GetMapping("/list")
    public Result getList(LiveUserParam param){
        IPage<LiveUser> page =  liveUserService.getList(param);
        return Result.success(page);
    }

    @ApiOperation("新增业主")
    @PostMapping
    public Result addLiveUser(@RequestBody LiveUser liveUser){
        //查看用户名是否存在
        LambdaQueryWrapper<LiveUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LiveUser::getUsername,liveUser.getUsername());
        LiveUser one = liveUserService.getOne(wrapper);
        if (!Objects.isNull(one)){
            return Result.error(500,"用户名已经存在");
        }
        //用户名加密
        liveUser.setPassword(passwordEncoder.encode(liveUser.getPassword()));
        //新增用户名
        liveUserService.saveLiveUser(liveUser);
        return Result.success();
    }

    @ApiOperation("编辑业主")
    @PutMapping
    public Result editLiveUser(@RequestBody LiveUser liveUser){
        //查看用户名是否存在
        LambdaQueryWrapper<LiveUser> query = new LambdaQueryWrapper<>();
        query.eq(LiveUser::getUsername,liveUser.getUsername());
        LiveUser one = liveUserService.getOne(query);
        if(one!=null && one.getUserId()!=liveUser.getUserId()){
            return Result.error(500,"用户名已存在");
        }
        //编辑用户
        liveUserService.editLiveUser(liveUser);
        return Result.success();
    }

    @ApiOperation("查看业主回显信息")
    @GetMapping("/getUserById/{userId}")
    public Result getUserById(@PathVariable("userId") Integer userId){
        LiveUser user = liveUserService.getUser(userId);
        return Result.success(user);
    }

    @ApiOperation("分配房屋保存")
    @PostMapping("/assignHouse")
    public Result assignHouse(@RequestBody AssignHouseParam param){
        liveUserService.assignHouse(param);
        return Result.success();
    }

    @ApiOperation("退房")
    @PostMapping("/returnHouse")
    public Result returnHouse(@RequestBody AssignHouseParam param){
        /*System.out.println("========="+param);*/
        //先检查电费水费是否缴费
        //电费
        QueryWrapper<FeePower> queryPower = new QueryWrapper<>();
        queryPower.lambda()
                .eq(FeePower::getUserId,param.getUserId())
                .eq(FeePower::getPayPowerStatus,0);
        List<FeePower> powerList = feePowerService.list(queryPower);
        if (powerList!=null && powerList.size()>0){
            return Result.error(500,"请缴完电费之后在退房");
        }
        //水费
        QueryWrapper<FeeWater> queryWater = new QueryWrapper<>();
        queryWater.lambda().eq(FeeWater::getUserId,param.getUserId())
                .eq(FeeWater::getPayWaterStatus,0);
        List<FeeWater> waterList = feeWaterService.list(queryWater);
        if(waterList!=null && waterList.size()>0){
            return Result.error(500,"请缴完水费之后在退房");
        }

        //把状态设置为0
        liveUserService.returnHouse(param);
        return Result.success();
    }

    //退房之后再删除
    @ApiOperation("删除业主")
    @DeleteMapping(value = {"/{userId}/{houseId},/{userId}"})
    public Result deleteUse(@PathVariable("userId") Integer userId,
                            @PathVariable(value = "houseId",required = false)
                            Integer houseId){
        //1.查询用户是否退房
        //状态为0才是退房
        QueryWrapper<LiveHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LiveHouse::getUserId,userId)
                .eq(LiveHouse::getHouseId,houseId)
                .eq(LiveHouse::getUseStatus,1);
        List<LiveHouse> list = liveHouseService.list(queryWrapper);
        if(list!=null && list.size()>0){
            return Result.error(500,"请退房后在删除业主");
        }

        //2.删除业主
        boolean remove = liveUserService.removeById(userId);
        if(remove){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }

}
